package com.example.bluetoothchat.utils;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;

public class BlueToothController {

    private BluetoothAdapter mAdapter;
    private Context context;

    public BluetoothAdapter getmAdapter() {
        return mAdapter;
    }

    public BlueToothController(Context context) {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        this.context = context;
    }

    public boolean isSupportBlueTooth() {
        if (mAdapter != null)
            return true;
        return false;
    }

    public boolean getBlueToothStatues(){
        assert (mAdapter != null);
        return mAdapter.isEnabled();
    }

    public void turnOnBlueTooth(Activity activity,int requestCode){
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(intent,requestCode);
        if (mAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
            Intent discover = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discover.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,120);
            context.startActivity(discover);
        }
    }

    public void turnOffBlueTooth(){
        mAdapter.disable();
    }
}
